﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;

namespace LDTB
{
    public partial class CatObject : Form
    {

        LDTBDataDataContext ds = DataService.DB;

        public CatObject()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int catid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            CatObjectEdit ve = new CatObjectEdit(catid);
            ve.ShowDialog();
            FillToFrom();

        }

        private void button1_Click(object sender, EventArgs e)
        {

            CatObjectAdd coa = new CatObjectAdd();
            coa.ShowDialog();
            FillToFrom();


        }

        private void FillToFrom()
        {
            var catobs = from c in ds.CateObjects
                         select c;

            this.dataGridView1.DataSource = catobs;

        }

        private void CatObject_Load(object sender, EventArgs e)
        {
            FillToFrom();


        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {

            int idcat = (int)dataGridView1.CurrentRow.Cells[0].Value;

            var people = from p in ds.Peoples
                         join c in ds.OptionCates
                         on p.CatID equals c.ID
                         where c.CateDisableID == idcat
                         select p;

            var cats = (from cat in ds.CateObjects
                      where cat.CatID == idcat
                      select cat).FirstOrDefault();

            DialogResult dr = MessageBox.Show("Có " + people.Count() + " người thuộc mục này có chắc nếu xóa sẽ mất hết người này  ", "Xóa mục "+cats.CatName, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                ds.Peoples.DeleteAllOnSubmit(people);
                ds.CateObjects.DeleteOnSubmit(cats);
                ds.SubmitChanges();
                FillToFrom();
                MessageBox.Show("Đã xóa thành công ");
            }
        }
    }
}
